package com.changgou.business.controller;

import com.changgou.business.pojo.Activity;
import com.changgou.business.service.ActivityService;
import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/activity")
public class ActivityController {
    //注入service
    @Autowired
    private ActivityService activityService;
    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Activity> activityList = activityService.finAll();
        return  new Result(true, StatusCode.OK, "查询成功",activityList);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findbyId(@PathVariable Integer id){
        Activity activity = activityService.findById(id);
        return  new Result(true, StatusCode.OK, "查询成功",activity);
    }

    /**
     * 新增数据
     * @param activity
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Activity activity){
     activityService.add(activity);
        return  new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改数据
     * @param activity
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Activity activity,@PathVariable Integer id){
        activity.setId(id);
        activityService.update(activity);
        return  new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id){
        activityService.delete(id);
        return  new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 多条件查询
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List list = activityService.findList(searchMap);
        return  new Result(true, StatusCode.OK, "查询成功",list);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable int page,@PathVariable int size){
        Page<Activity> page1 = activityService.findPage(page, size);
        PageResult pageResult = new PageResult(page1.getTotal(), page1.getResult());
        return  new Result(true, StatusCode.OK, "查询成功",page1);
    }

    /**
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/searchPage/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap,@PathVariable int page,@PathVariable int size){
        Page<Activity> page1 = activityService.findPage(searchMap,page, size);
        PageResult pageResult = new PageResult(page1.getTotal(), page1.getResult());
        return  new Result(true, StatusCode.OK, "查询成功",page1);
    }
}
